package com.baizhi.controller;


import com.baizhi.domain.Result;
import com.baizhi.domain.StatusCode;
import com.baizhi.entity.CmfzArticle;
import com.baizhi.entity.CmfzGuru;
import com.baizhi.service.CmfzArticleService;
import com.github.pagehelper.PageInfo;
import org.apache.poi.hssf.record.ObjRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ljh
 * @since 2020-08-18
 */
@RestController
@RequestMapping("/cmfzArticle")
public class CmfzArticleController {

    @Autowired
    private CmfzArticleService cmfzArticleService;

    /**
     * es搜素+分页查询
     * @param page
     * @param limit
     * @param key
     * @return
     */
    @RequestMapping("/selectArticleByPage")
    public Map<String,Object> selectArticleByPage(Integer page,Integer limit,
                                               @RequestParam(required = false) String key){
        HashMap<String, Object> map = new HashMap<>();
        System.out.println("es+分页");
        try{
            PageInfo<CmfzArticle> pageInfo=null;
            if (StringUtils.isEmpty(key)||key==null){
                pageInfo=cmfzArticleService.selectAllArticle(page,limit);
            }else {
                pageInfo=cmfzArticleService.selectArticleByPage(page,limit,key);
            }
            System.out.println(pageInfo.getList());
            System.out.println(pageInfo.getTotal());
            map.put("code",0);
            map.put("count",pageInfo.getTotal());
            map.put("data",pageInfo.getList());
            map.put("msg","查询成功");
        }catch (Exception e){
            map.put("code",1);
            map.put("msg","查询失败");
        }
        return map;
    }

    /**
     * 逻辑删除文章
     * @return
     */
    @RequestMapping("/deleteArticle")
    public Result deleteArticle(Integer id){
        try {
            cmfzArticleService.deleteArticle(id);
            return new Result(true, StatusCode.OK,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"逻辑删除失败");
        }
    }
}
